package com.ruix.app.ui.base;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ruix.app.RuiApplication;
import com.ruix.app.constant.RuiXConstant;
import com.ruix.app.network.exception.NetworkUnusableException;
import com.ruix.app.network.exception.TokenExpiredException;
import com.ruix.app.util.ToastUtil;

import java.io.IOException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;


/**
 * Created by hadexs on 2017/4/10.
 */

public abstract class BaseFragment extends Fragment {

    public View mFragmentView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mFragmentView = inflater.inflate(loadViewLayout(), null);
        init();// 初始化
        return mFragmentView;
    }

    // 加载布局文件
    protected abstract int loadViewLayout();

    private void init() {
        initTitleBar();// 初始化标题栏
        initView(); // 初始化控件
        initListener();// 设置监听器
        initPresenter(); // 初始化presenter
        initLocalData();// 初始化数据
        initBaseProperty();//加载网络数据
//        if (presenter != null) { // 绑定事件
//            presenter.onCreate();
//        }
    }

    // 初始化标题栏
    protected void initTitleBar() {
        if (!isUseBaseTitleBar()) {// 是否使用基础类提供的标题栏
            return;
        }

//        titleBar = (CommonTitleBar) mFragmentView.findViewById(R.id.nav);
//        if (titleBar != null) {
//            titleBar.setOnClickLeftListener(this);
//            titleBar.setOnClickRightListener(this);
//        }
    }

    // 是否使用基础类提供的标题栏
    protected boolean isUseBaseTitleBar() {
        return true;
    }

    // 初始化presenter
    protected void initPresenter() {
    }

    // 初始化控件
    protected abstract void initView();

    // 初始化数据
    protected abstract void initLocalData();

    // 初始化预先加载数据
    protected void initBaseProperty() {
    }

    // 设置监听器
    protected abstract void initListener();


    public void showSuccessTip(String msg) {
        ToastUtil.showNewSuccessMsg(msg);
    }

    public void showSuccessTip(int msgResId) {
        ToastUtil.showNewSuccessMsg(msgResId);
    }

    public void showErrorTip(String msg) {
        ToastUtil.showNewErrorMsg(msg);
    }

    public void showErrorTip(int msgResId) {
        ToastUtil.showNewErrorMsg(msgResId);
    }

    public void showLoading() {// 当前Fragment所在Activity必须为BaseActivity
        if (getActivity() != null) {
            ((BaseActivity) getActivity()).showLoading(true);
        }
    }


    public void dismissLoading() {// 当前Fragment所在Activity必须为BaseActivity
        if (getActivity() != null) {
            ((BaseActivity) getActivity()).dismissLoading();
        }
    }

    public void resolveException(Throwable e){
        if (e instanceof SocketTimeoutException) {
            showErrorTip("网络在开小差，检查后再试吧");
        } else if (e instanceof UnknownHostException) { //解析不到域名 网络不通
            showErrorTip("网络在开小差，检查后再试吧");
        } else if (e instanceof NetworkUnusableException) { // 网络不可用，比如WiFi断开
            showErrorTip("网络在开小差，检查后再试吧");
        } else if (e instanceof IOException) { //socket 超时 网络超时 SocketTimeoutException
            showErrorTip("网络在开小差，检查后再试吧");
            e.printStackTrace();
        } else if (e instanceof TokenExpiredException){ //重新登录
            Intent intent = new Intent();
            intent.setAction(RuiXConstant.NOTICE_LOGOUT);
            RuiApplication.getAppContext().sendOrderedBroadcast(intent, null);
        } else {
            showErrorTip(e.getMessage());
        }
    }


}
